<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="投资人" prop="investName">
        <el-input
          v-model="queryParams.investName"
          placeholder="请输入投资人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分红状态" prop="dividendVerify">
        <el-select v-model="queryParams.dividendVerify" placeholder="请选择分红状态" clearable size="small">
          <el-option
            v-for="dict in dividendVerifyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dividendList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="投资人" align="center" prop="investName" />
      <el-table-column label="分红金额" align="center" prop="dividendNumber" />
      <el-table-column label="分红信息" align="center" prop="dividendMessage" />
      <el-table-column label="分红状态" align="center" prop="dividendVerify" :formatter="dividendVerifyFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.dividendVerify === 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.dividendVerify !== 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分红对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="投资人" prop="investName">
          <el-select v-model="department" placeholder="请输入投资人" clearable size="small" @change="changeThePublish(department)">
            <el-option
              v-for="item in DividendStockList"
              :key="item.id"
              :label="item.applicant"
              :value="item"
            >
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="分红金额" prop="dividendNumber">
          <el-input v-model="form.dividendNumber" placeholder="请输入分红金额" />
        </el-form-item>
        <el-form-item label="分红信息" prop="dividendMessage">
          <el-input v-model="form.dividendMessage" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
        <el-button @click="submitForm">保 存</el-button>
        <el-button type="primary" @click="submit">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDividend, getDividend, delDividend, addDividend, updateDividend, exportDividend, myListDividend } from "@/api/publish/dividend";
import { selectDividendStockList } from "@/api/invest/stockApply";

export default {
  name: "Dividend",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分红表格数据
      dividendList: [],
      // 申购委托表格数据
      DividendStockList: [],
      department: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分红状态字典
      dividendVerifyOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        publishId: null,
        publishName: null,
        investId: null,
        investName: null,
        dividendId: null,
        dividendNumber: null,
        dividendMessage: null,
        dividendVerify: null,
        deptId: null,
        createById: null,
        updateById: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        investName: [
          { required: true, message: "投资人不能为空", trigger: "change" }
        ],
        dividendNumber: [
          { required: true, message: "分红金额不能为空", trigger: "blur" }
        ],
        dividendMessage: [
          { required: true, message: "分红信息不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("dividend_verify").then(response => {
      this.dividendVerifyOptions = response.data;
    });
  },
  methods: {
    /** 查询分红列表 */
    getList() {
      this.loading = true;
      myListDividend(this.queryParams).then(response => {
        this.dividendList = response.rows;
        this.total = response.total;
      });
      selectDividendStockList().then(response => {
        this.DividendStockList = response.rows;
        this.loading = false;
      });
    },
    // 分红状态字典翻译
    dividendVerifyFormat(row, column) {
      return this.selectDictLabel(this.dividendVerifyOptions, row.dividendVerify);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        publishId: null,
        publishName: null,
        investId: null,
        investName: null,
        dividendId: null,
        dividendNumber: null,
        dividendMessage: null,
        dividendVerify: null,
        deptId: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null,
        remark: null
      };
      this.department = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.dividendVerify = "0";
      this.open = true;
      this.title = "添加分红";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDividend(id).then(response => {
        this.form = response.data;
        this.department = {
          applicant: this.form.investName,
          applicantId: this.form.investId,
        };
        this.open = true;
        this.title = "修改分红";
      });
    },

    // 根据发行人id查投资人信息
    changeThePublish(item) {
      if (item) {
        this.form.investId = item.applicantId;
        this.form.investName = item.applicant;
      }
    },
    //提交
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.dividendVerify = "1";
          if (this.form.id != null) {
            updateDividend(this.form).then(response => {
              this.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDividend(this.form).then(response => {
              this.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 保存按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDividend(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDividend(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除分红编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDividend(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
  }
};
</script>
